package pit.com.ordermobilesystem.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.util.Utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StoreAdapter extends BaseAdapter{
	JSONArray arrayStores;
	private Context mContext;
	DisplayImageOptions options;	
	public ImageLoader imageLoader = ImageLoader.getInstance();
	public StoreAdapter(JSONArray arrayStores, Context mContext) {
		super();
		this.arrayStores = arrayStores;
		this.mContext = mContext;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_no_image)
		.showImageForEmptyUri(R.drawable.ic_no_image)
		.showImageOnFail(R.drawable.ic_no_image)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();	
	}

	@Override
	public int getCount() {
		return arrayStores.length();
	}

	@Override
	public Object getItem(int position) {
		try {
			return arrayStores.get(position);
		} catch (JSONException e) {
			
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			LayoutInflater li = (LayoutInflater)mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=li.inflate(R.layout.view_row_store, null, true);
		}else{
			
		}
		JSONObject store=(JSONObject) getItem(position);
		TextView tv_name=(TextView) convertView.findViewById(R.id.tv_name);
		TextView tv_location=(TextView) convertView.findViewById(R.id.tv_location);
		TextView tv_phone=(TextView) convertView.findViewById(R.id.tv_phone);
		ImageView img_detail=(ImageView) convertView.findViewById(R.id.img_detail);
		try {
			tv_name.setText(store.getString("store_name"));
			tv_location.setText(store.getString("store_address"));
			tv_phone.setText(store.getString("store_mobile"));
			imageLoader.displayImage(Utils.getPathImage(store.getString("store_image")), img_detail, options, null);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return convertView;
	}



}
